/**
 * 复制克隆一个数据对象
 * @param obj 数据对象
 * @returns 复制结果
 */
export function CloneObject(obj: any) {
    // Handle null or undefined or function
    if (null == obj || "object" != typeof obj)
        return obj;
    // Handle the 3 simple types, Number and String and Boolean
    if (obj instanceof Number || obj instanceof String || obj instanceof Boolean)
        return obj.valueOf();
    // Handle Date
    if (obj instanceof Date) {
        var copy = new Date();
        copy.setTime(obj.getTime());
        return copy;
    }
    // Handle Array or Object
    if (obj instanceof Object || obj instanceof Array) {
        var copy1 = (obj instanceof Array) ? [] : {};
        for (var attr in obj) {
            if (obj.hasOwnProperty(attr))
                copy1[attr] = obj[attr] ? CloneObject(obj[attr]) : obj[attr];
        }
        return copy1;
    }
    throw new Error("Unable to clone obj! Its type isn't supported.");
}

/**
 * 合并参数入URL地址
 * @param url URL访问字符串路径地址
 * @param key 参数名称
 * @param value 参数值
 * @returns 合并结果
 */
export function UrlParam(url: string, key: string, value: any) {
    if (value == undefined || isNaN(value) || value == null) {
        value = '';
    }
    if (value instanceof Array) {
        var num2 = url.indexOf("?");
        if (num2 < 0) {
            url = url + "?";
        }
        url = url + $.map(value, function (v) { return key + "=" + v; }).join('&');
        return url;
    }
    var str = encodeURIComponent(value);
    var index = url.indexOf("#");
    var str2 = "";
    if (index > -1) {
        str2 = url.substring(index);
        url = url.substring(0, index - 1);
    }
    var num2 = url.indexOf("?");
    if (num2 < 0) {
        url = url + "?" + key + "=" + str;
    } else if (num2 == (url.length - 1)) {
        url = url + key + "=" + str;
    } else {
        var regex = new RegExp(key + "=[^\\s&#]*", "gi");
        if (regex.test(url)) {
            url = url.replace(regex, key + "=" + str);
        }
        else {
            var str4 = url;
            url = str4 + "&" + key + "=" + str;
        }
    }
    return (url + str2);
}

/** moment 时间插件格式字符串 */
export const moment_format = {
    /** 日常版: YYYY-MM-DD HH:mm:ss */
    normal: "YYYY-MM-DD HH:mm:ss",
}
